// import sendEvent, {finishEvents,goodsDetailEvents} from "./eventupload";
import { alert, toast } from "./modal";
// import { _requestSubscribeMessage } from "./_requestSubscribeMessage";
// import request from "./request";
// import { URL_SUBSCRIBE } from "./config";
let userInfo = wx.getStorageSync('userInfo') || {};
/**
 * 支付成功之后的订阅
 * @param {*} orderInfo
 */
export const onSubscribe = (orderInfo) => {
  let self = this
  let eventParams = {
    is_new: userInfo.is_new || false,
    taskId: orderInfo.no,
    type: 'payment'
  }
  // sendEvent(finishEvents.clickSubscribe(eventParams));
  let isSubscribe = wx.getStorageSync(`subscribePaymentSuc${orderInfo.no}`) || false
  if (isSubscribe) {
    // toast("已订阅");
    return
  }
  let tmplIds = ['SdecCNFVXz-L8srDBMsIuQbSRnEl7rJ1613sT0iOIa4', 'FhqLSw6YodvdhG5dJu3cUYwEfbib0J6IvlfNzJ7oYso', '59-JT35dSprGMUXVKrYQdv-x8Rso3bWhhJ85i4NCS0A'] // 正式
  if (userInfo.is_new) {
    // 新用户
    tmplIds = ['SdecCNFVXz-L8srDBMsIuQbSRnEl7rJ1613sT0iOIa4', '5Ro9dous4yP6Etgo1OyN2_CP4G4ASddkALzjXQu91HI', 'KfIyczDjVf7-YCtLVesJa4FpsXi_7QWW4pu9HxL8gMc']
  }
  // _requestSubscribeMessage(tmplIds,
  //   (res, isAllow) => {
  //     console.log(res);
  //     if (res.errMsg === "requestSubscribeMessage:ok") {
  //       let isSubscribe = Object.values(res).includes('accept') // 是否订阅
  //       if (isSubscribe) {
  //         // sendEvent(finishEvents.subscribeSuc(eventParams));
  //         wx.setStorageSync(`subscribePaymentSuc${orderInfo.no}`, true)
  //         request.httpPost(URL_SUBSCRIBE, {
  //           resource_type: 'payment',
  //           resource_id: orderInfo.no
  //         }, {}, {
  //           showLoading: false
  //         })
  //       } else {
  //         // sendEvent(finishEvents.subscribeRefuse(eventParams));
  //       }
  //     }
  //   }, (err) => {
  //     // sendEvent(finishEvents.subscribeFail(eventParams));
  //   }, (com) => {
  //   })
}

/**
 * 微信支付
 * @param {*} orderInfo
 */

// 支付成功默认回调
export const paySuccess = (orderInfo) => {
  // let redEnvelope = orderInfo.red_envelope ? JSON.stringify(orderInfo.red_envelope) : null;
  // let addressInfo = JSON.stringify(orderInfo.address)
  // let is_audit_dist = orderInfo.is_audit_dist || ''
  // let showCouponList = orderInfo.showCouponList || '' // 是否显示优惠券列表
  let order_type = orderInfo.order_type || 'order'
  if (order_type == 'vip') return
  wx.redirectTo({
    url: `/pages/order/order`
  })
}

// 支付失败默认回调
export const payFail = (orderInfo, err) => {
  let order_type = orderInfo.order_type || 'order'
  // if (err.errMsg == "requestPayment:fail cancel") {
  //   //用户取消支付
  //   // if (order_type == 'vip') return
  //   // wx.redirectTo({
  //   //   url: `/pages/order/order`
  //   // })
  // } else {
  //支付异常
  wx.showToast({
    title: '支付异常,请联系客服',
    icon: "none",
    duration: 2000,
    mask: true,
  })
  // if (order_type == 'vip') return
  // setTimeout(function () {
  //   wx.redirectTo({
  //     url: `/pages/order/order`
  //   })
  // }, 2000)
  // }
}

// 微信支付
export const wxPay = (orderInfo, callbackSuc, callBackFail) => {
  const { timeStamp, nonceStr, signType, paySign } = orderInfo
  console.log(orderInfo)
  wx.requestPayment({
    timeStamp: timeStamp + "",
    nonceStr: nonceStr + "",
    package: orderInfo.package,
    signType: signType,
    paySign: paySign,
    success(res) {
      console.log(res)
      callbackSuc && callbackSuc(res) || paySuccess(orderInfo)
    },
    fail(err) {
      console.log('支付失败')
      console.log(err)
      callBackFail && callBackFail(err) || payFail(orderInfo, err)
    }
  })
}
